/** 
 * Project Name:zju 
 * File Name:AdminController.java 
 * Package Name:cn.edu.zju.controller 
 * Date:2014年10月9日下午2:00:58 
 * Copyright (c) 2014, 杭州杰唐信息技术有限公司 All Rights Reserved. 
 * 
 */
package com.hbjycl.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.task.TaskExecutor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;











import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hbjycl.entity.Admin;
import com.hbjycl.entity.Student;
import com.hbjycl.entity.Teacher;
import com.hbjycl.service.AdminService;
import com.hbjycl.service.StudentService;
import com.hbjycl.service.TeacherService;

/**
 * 用户管理
 * 
 * @author wxp
 *
 * @datetime 2014年10月9日下午2:00:58
 */
@Controller
public class TeacherController extends BaseController
{
    private static final Logger logger = LoggerFactory.getLogger(TeacherController.class);
    
    @Resource
    private TeacherService        teacherService;
    /*
     * 注册
     */
	@RequestMapping(value = "teacher/register", method = RequestMethod.POST)
    public ModelAndView register(
    		HttpSession httpsession,
            @RequestParam(value = "username", required = false, defaultValue = "") String username,
            @RequestParam(value = "password", required = false, defaultValue = "") String password,
            @RequestParam(value = "mobile", required = false, defaultValue = "") String mobile,
            @RequestParam(value = "email", required = false, defaultValue = "") String email,
            @RequestParam(value = "name", required = false, defaultValue = "") String name
         ){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("index");
        Teacher teacher=teacherService.getTeacherByUsername(username);
        Teacher newsteacher=new Teacher();
        if(teacher==null)
        {   	
        	newsteacher.setUsername(username);
        	newsteacher.setPassword(password);
        	newsteacher.setMobile(mobile);
        	newsteacher.setEmail(email);
        	newsteacher.setName(name);
        	newsteacher.setIsAccountEnabled(true);
        	newsteacher.setCreateDate(new Date());
        	newsteacher.setIsAccountExpired(false);
        	newsteacher.setIsAccountLocked(false);
        	newsteacher.setLoginDate(new Date());
        	newsteacher.setLoginFailureCount(0);
        	try{
        		teacherService.save(newsteacher);
                mav.addObject("teacher", newsteacher);
                httpsession.setAttribute("username", newsteacher.getUsername());
        	}
          catch(Exception e)
        	{
        	  System.out.println(e);
        	  mav.addObject(TIP_ERROR,e);
          	  mav.setViewName("register");
        	}
        }
        else
        {
        	mav.addObject(TIP_ERROR,"用户名已存在");
        	mav.setViewName("register");
        }
        return mav;
    }	
	  /*
     * 登录
     */
	   @RequestMapping(value = "teacher/login", method = RequestMethod.POST)
	    public ModelAndView login(HttpSession httpsession,
	    		@RequestParam(value = "username", required = false, defaultValue = "") String username,
	            @RequestParam(value = "password", required = false, defaultValue = "") String password)
	    {
	        ModelAndView mav = new ModelAndView();
	        mav.setViewName("index");
	        Teacher teacher=teacherService.getTeacherByUsername(username);
	        if(teacher==null)
	        {
	        	mav.addObject(TIP_ERROR, "用户名不存在");
	        	mav.setViewName("login");
	        }
	        else if(password.equals(teacher.getPassword()))
	        {
	        	mav.addObject("teacher", teacher);
	        	mav.addObject(TIP_INFO, "登陆成功");
	        	httpsession.setAttribute("username", teacher.getUsername());
	        }
	        else
	        {
	        	mav.addObject(TIP_ERROR, "用户名或密码错误");
	        	mav.setViewName("login");
	        }
	        return mav;
	    }
    
}
